<?php

/**
 * memcache构建的简单内存队列
 * 
 * @author: suwenxiong
 */
 
 

 
 


$a = new memList('sqqq');
for($i=0; $i<10000;$i++){
    $a->in("kk".$i);
}
$a->out();
echo $a->data;




/**
 * @author suwenxiong
 * memcache构建的简单内存队列
 */
class memList
{
    public $memcache; // memcache类

    public $queKeyPrefix; //数据键前缀
    public $startKey; //开始指针键
    public $endKey; //结束指针键
    
    public $data; //出队列取出的数据
    
    public $time;  //数据过期时间

    public function __construct($key)
    {
        $this->memcache = new Memcache;
        $this->memcache->connect('127.0.0.1', 11211);
        $this->queKeyPrefix = "MEMQUE_{$key}_";
        $this->startKey = "MEMQUE_SK_{$key}";
        $this->endKey = "MEMQUE_EK_{$key}";
        $this->time = 7*24*3600;

    }

    /**
     * 获取列表
     *  先拿到开始结束指针， 然后去拿数据
     * 
     * @return array
     */
    public function getList()
    {
        $startP = $this->memcache->get($this->startKey);
        $endP = $this->memcache->get($this->endKey);
        empty($startP) && $startP = 0;
        empty($endP) && $endP = 0;
        $arr = array();
        for ($i = $startP; $i < $endP; ++$i) {
            $key = $this->queKeyPrefix . $i;
            $arr[] = $this->memcache->get($key);
        }
        return $arr;
    }

    /**
     * 插入队列
     *   结束指针后移,拿到一个自增的id
     *   再把值存到指针指定的位置
     *   
     *   @return void
     */
    public function in($value)
    {

        if ($this->memcache->get($this->endKey) === false) {
            $index = 0;
            $this->memcache->set($this->startKey, 0, false ,$this->time);
            $this->memcache->set($this->endKey, 0, false,  $this->time);
        }
        $index = $this->memcache->increment($this->endKey);


        $key = $this->queKeyPrefix . ($index - 1);
        $this->memcache->set($key, $value, false, $this->time);
        return $key;
    }

    /**
     * 出队
     *    很简单, 把开始值取出后开始指针后移
     * 
     * @return mixed
     */
    public function out()
    {
        $start = $this->memcache->get($this->startKey);
        $this->data = $this->memcache->get($this->queKeyPrefix . $start);
        $end = $this->memcache->get($this->endKey);
        $this->memcache->delete($this->queKeyPrefix.$start);
        if ($start < $end) {
            $this->memcache->increment($this->startKey);
            return true;            
        }else{
            return false;
        }
        

    }
    
}

class memList22
{
    public $memcache; // memcache类

    public $queKeyPrefix; //数据键前缀
    public $startKey; //开始指针键
    public $endKey; //结束指针键
    
    public $data;

    public function __construct($key)
    {
        $this->memcache = new Memcache;
        $this->memcache->connect('127.0.0.1', 11211);
        $this->queKeyPrefix = "MEMQUE_{$key}_";
        $this->startKey = "MEMQUE_SK_{$key}";
        $this->endKey = "MEMQUE_EK_{$key}";

    }

    /**
     * 获取列表
     *  先拿到开始结束指针， 然后去拿数据
     * 
     * @return array
     */
    public function getList()
    {
        $startP = $this->memcache->get($this->startKey);
        $endP = $this->memcache->get($this->endKey);
        empty($startP) && $startP = 0;
        empty($endP) && $endP = 0;
        $arr = array();
        for ($i = $startP; $i < $endP; ++$i) {
            $key = $this->queKeyPrefix . $i;
            $arr[] = $this->memcache->get($key);
        }
        return $arr;
    }

    /**
     * 插入队列
     *   结束指针后移,拿到一个自增的id
     *   再把值存到指针指定的位置
     *   
     *   @return void
     */
    public function in($value)
    {

        if ($this->memcache->get($this->endKey) === false) {
            $index = 0;
            $this->memcache->set($this->startKey, 0);
            $this->memcache->set($this->endKey, 0);
        }
        $index = $this->memcache->increment($this->endKey);


        $key = $this->queKeyPrefix . ($index - 1);
        $this->memcache->set($key, $value);
        return $key;
    }

    /**
     * 出队
     *    很简单, 把开始值取出后开始指针后移
     * 
     * @return mixed
     */
    public function out()
    {
        $start = $this->memcache->get($this->startKey);
        $this->data = $this->memcache->get($this->queKeyPrefix . $start);
        $end = $this->memcache->get($this->endKey);
        $this->memcache->delete($this->queKeyPrefix.$start);
        if ($start < $end) {
            $this->memcache->increment($this->startKey);
            return true;            
        }else{
            return false;
        }
        

    }
    
}
